<?php
// this check prevents access to debug front conrollers that are deployed by accident to production servers.
// feel free to remove this, extend it or make something more sophisticated.
if (!in_array(@$_SERVER['REMOTE_ADDR'], array('127.0.0.1', '::1')))
{
//  die('You are not allowed to access this file. Check '.basename(__FILE__).' for more information.');
}

require_once(dirname(__FILE__).'/../config/ProjectConfiguration.class.php');

$configuration = ProjectConfiguration::getApplicationConfiguration('frontend', 'prod', true);
sfContext::createInstance($configuration);

$databaseManager = new sfDatabaseManager($configuration);

// $set = new ezcMailFileSet(array($argv[1]));
// $file = dirname(__FILE__).'/../data/messages/1235865791.V803Ic5034aM33439.ip-208-109-110-152.ip.secureserver.net';	// debug
if(isset($argv[1])) {
	$file = $argv[1];
	$result = MessageParser::parse($file);
	$basedir = dirname($file);
	$filename = basename($file);
	
	if($result) {
		echo "parsed file successfully, moving to cur folder \n";
		// move the message to the cur folder
		$targetDir = $basedir.'/../cur';		
	} else {
		echo "parsed file un-successfully, moving to tmp folder \n";		
		// move the message to the tmp folder		
		$targetDir = $basedir.'/../tmp';
	}
	
	if(!is_dir($targetDir)) {
		mkdir($targetDir, 0777, true);			
	}
	rename($file, $targetDir.'/'.$filename);
	echo "moved file to: ".$targetDir.'/'.$filename;
	echo "\n\n";
} else {
	echo "syntax: php ratl_parser.php [filename] \n\n";
}

